<template>
	<view class="page">
		<view>
			<u-notice-bar text="长按可删除"></u-notice-bar>
		</view>
		<u-modal :show="show" title="删除用户" content='确定删除该用户？' @confirm="deleteItem" showCancelButton="true"
			@cancel="cancelDelete"></u-modal>
		<view style="padding: 20rpx;"><u-search @search="search" @custom="search" placeholder="请输入搜索内容"
				v-model="searchValue"></u-search></view>
		<u-empty v-if="list.length == 0" mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png">
		</u-empty>
		<uni-list>
			<uni-list-chat v-for="one in list" :key="one.id" :title="one.nickname + (one.is_deleted == 0?'':'(已删除)')"
				:note="one.name" :avatar="one.avatar" badgePositon="left"
				:to="'/pages/update_user/update_user?userId=' + one.id" @longpress="showModal(one.id)">
				<view class="chat-custom-right">
				</view>
			</uni-list-chat>
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				searchValue: '',
				list: [],
				page: 1,
				length: 12,
				isLastPage: false,
				userId: '',
			}
		},
		onPullDownRefresh() {
			let that = this;
			setTimeout(() => {
				uni.redirectTo({
					url:'/pages/user_managment/user_managment'
				})
			}, 1000)
		},
		onLoad() {
			let that = this;
			that.loadUser(that);
		},
		onReachBottom: function() {
			let that = this
			if (that.isLastPage) {
				return
			}
			that.page = that.page + 1
			that.loadUser(that)
		},
		methods: {
			cancelDelete() {
				this.show = false;
			},
			deleteItem() {
				let that = this;
				that.$ajax(that.$url.deleteUser + "?userId=" + that.userId, "GET", null, function(resp) {
					uni.showToast({
						icon: 'none',
						title: '删除成功！'
					})
					that.show = false;
					uni.redirectTo({
						url: '/pages/user_managment/user_managment'
					})
				})
			},
			showModal(id) {
				let that = this;
				that.userId = id;
				// 触发震动效果
				uni.vibrateShort();
				that.show = true;
			},
			search() {
				let that = this;
				that.list = [];
				that.loadUser(that);
			},
			loadUser(ref) {
				let that = this;
				let data = {
					start: ref.page,
					length: ref.length
				}
				if (that.searchValue != '' && that.searchValue != null) {
					data = {
						start: 1,
						length: 100,
						searchValue: that.searchValue
					}
				}
				ref.$ajax(ref.$url.searchAllUserByPage, "POST", data, function(resp) {
					let result = resp.data.result
					that.searchValue = '';
					if (result == null || result.length == 0) {
						ref.isLastPage = true
						ref.page = ref.page - 1
						uni.showToast({
							icon: "none",
							title: "已经到底了"
						})
					} else {
						if (ref.page == 1) {
							ref.list = []
						}
						ref.list = ref.list.concat(result)
						if (ref.page > 1) {
							uni.showToast({
								icon: "none",
								title: "又加载了" + result.length + "条消息"
							})
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>